﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Xml;
using System.Xml.Linq;
using CustomControl;
using TeacherLeftHand.Model;
using log4net;
using TeacherLeftHand.Util;
using System.Windows.Controls;

namespace TeacherLeftHand.DataAccess
{
    class TeachingPlanAccessor
    {
        #region Variables

        protected static readonly ILog Log = LogManager.GetLogger(typeof(MainWindow));
        public static TeachingPlan TeachingPlan;

        #endregion
        
        #region Constructors
        public TeachingPlanAccessor()
        {
            //_subjectList = new ObservableCollection<string>();
            //KIList = new ObservableCollection<KnowledgeItem>();
            //SelectedSubject = "";
            //GetSubjectList();
        }
        #endregion

        #region Methods

        /// <summary>
        /// SaveTeachingPlan Teaching Plan Data in a TLHX file.
        /// </summary>
        /// <param name="filePath">Specify the path to save file</param>
        /// <param name="teachingPlanObj">Specify the Teaching Plan Object</param>
        /// <returns>Boolean</returns>
        public static bool Save(string filePath, TeachingPlan teachingPlanObj)
        {
            try
            {
                var xdoc = new XmlDocument();
                var teachingPlanNode = xdoc.CreateElement("TeachingPlan");
                //composingDateNode.InnerText = comDate;
                xdoc.AppendChild(teachingPlanNode);  

                //xdoc.LoadXml(@"<TeachingPlan></TeachingPlan>");
                //var teachingplan = new XElement("TeachingPlan");

                string xmlTree;
                string comDate = null;
                if(teachingPlanObj.ComposeDate == null)
                {
                    comDate = "";
                }
                else if (string.IsNullOrEmpty(teachingPlanObj.ComposeDate.Value.ToString(CultureInfo.InvariantCulture)))
                {
                    comDate = "";
                }
                else
                {
                    comDate = teachingPlanObj.ComposeDate.Value.ToString("MM/dd/yyyy");
                }
                //var composingDate = new XElement("ComposingDate", comDate);
                var composingDateNode = xdoc.CreateElement("ComposingDate");
                composingDateNode.InnerText = comDate;
                teachingPlanNode.AppendChild(composingDateNode);               
                //teachingplan.Add(composingDate);
                //xdoc.FirstChild.nod
                string teaDate = null;
                if (teachingPlanObj.TeachingDate == null)
                {
                    teaDate = "";
                }
                else if (string.IsNullOrEmpty(teachingPlanObj.TeachingDate.Value.ToString(CultureInfo.InvariantCulture)))
                {
                    teaDate = "";
                }
                else
                {
                    teaDate = teachingPlanObj.TeachingDate.Value.ToString("MM/dd/yyyy");
                }
                //var teachingDate = new XElement("TeachingDate", teaDate);
                //teachingplan.Add(teachingDate);
                var teachingDateNode = xdoc.CreateElement("TeachingDate");
                teachingDateNode.InnerText = teaDate;
                teachingPlanNode.AppendChild(teachingDateNode);
                //var classLevel = new XElement("ClassLevel", Convert.ToString(teachingPlanObj.ClassLevel));
                //teachingplan.Add(classLevel);
                var classLevelNode = xdoc.CreateElement("ClassLevel");
                classLevelNode.InnerText = Convert.ToString(teachingPlanObj.ClassLevel);
                teachingPlanNode.AppendChild(classLevelNode);
                //var subject = new XElement("Subject", teachingPlanObj.Subject);
                //teachingplan.Add(subject);
                var subjectNode = xdoc.CreateElement("Subject");
                subjectNode.InnerText = teachingPlanObj.Subject;
                teachingPlanNode.AppendChild(subjectNode);
                //var session = new XElement("Session", Convert.ToString(teachingPlanObj.Session));
                //teachingplan.Add(session);
                var sessionNode = xdoc.CreateElement("Session");
                sessionNode.InnerText = Convert.ToString(teachingPlanObj.Session);
                teachingPlanNode.AppendChild(sessionNode);
                //var week = new XElement("Week", Convert.ToString(teachingPlanObj.Week));
                //teachingplan.Add(week);
                var weekNode = xdoc.CreateElement("Week");
                weekNode.InnerText = Convert.ToString(teachingPlanObj.Week);
                teachingPlanNode.AppendChild(weekNode);
                //XElement topic = new XElement("Topic", new XAttribute("Number", "35"), "Luyện Tập Tả Cảnh");
                //var topic = new XElement("Topic",new XAttribute("Number", Convert.ToString(teachingPlanObj.TopicNo)), teachingPlanObj.Topic);
                //teachingplan.Add(topic);
                var topicNode = xdoc.CreateElement("Topic");
                topicNode.SetAttribute("Number","",Convert.ToString(teachingPlanObj.TopicNo));
                topicNode.InnerText = teachingPlanObj.Topic;
                teachingPlanNode.AppendChild(topicNode);
                //var objective = new XElement("Objective", teachingPlanObj.Objective);
                //teachingplan.Add(objective);

                //var objectiveNode = xdoc.CreateElement("Objective");
                //objectiveNode.InnerText = teachingPlanObj.Objective;
                //teachingPlanNode.AppendChild(objectiveNode);

                var objectiveNode = xdoc.CreateElement("Objective");
                var xfragObjectiveNode = xdoc.CreateDocumentFragment();
                xfragObjectiveNode.InnerXml = (string)teachingPlanObj.Objective;
                objectiveNode.AppendChild(xfragObjectiveNode);
                teachingPlanNode.AppendChild(objectiveNode);
                //var preparation = new XElement("Preparation", teachingPlanObj.Preparation);
                //teachingplan.Add(preparation);
                //var preparationNode = xdoc.CreateElement("Preparation");
                //preparationNode.InnerText = teachingPlanObj.Preparation;
                //teachingPlanNode.AppendChild(preparationNode);

                var preparationNode = xdoc.CreateElement("Preparation");
                var xfragPreparationNode = xdoc.CreateDocumentFragment();
                xfragPreparationNode.InnerXml = (string)teachingPlanObj.Preparation;
                preparationNode.AppendChild(xfragPreparationNode);
                teachingPlanNode.AppendChild(preparationNode);
                //var activities = new XElement("Activities", teachingPlanObj.Activities);
                //var activities = new XElement("Activities");
                var activitiesNode = xdoc.CreateElement("Activities");
                teachingPlanNode.AppendChild(activitiesNode);
                XElement activity;
                XElement name;
                XElement purpose;
                XElement form;
                XElement content;
                for (int i = 0; i < teachingPlanObj.Activities.Count; i++)
                {
                    //activity = new XElement("Activity");
                    var activityNode = xdoc.CreateElement("Activity");
                    activitiesNode.AppendChild(activityNode);
                    //name = new XElement("Name",teachingPlanObj.Activities.ElementAt(i).Name);
                    //activity.Add(name);
                    var nameNode = xdoc.CreateElement("Name");
                    nameNode.InnerText = teachingPlanObj.Activities.ElementAt(i).Name;
                    activityNode.AppendChild(nameNode);

                    //purpose = new XElement("Purpose", teachingPlanObj.Activities.ElementAt(i).Purpose);
                    //activity.Add(purpose);
                    var purposeNode = xdoc.CreateElement("Purpose");
                    purposeNode.InnerText = teachingPlanObj.Activities.ElementAt(i).Purpose;
                    activityNode.AppendChild(purposeNode);

                    //form = new XElement("Form", teachingPlanObj.Activities.ElementAt(i).Form);
                    //activity.Add(form);
                    var formNode = xdoc.CreateElement("Form");
                    formNode.InnerText = teachingPlanObj.Activities.ElementAt(i).Form;
                    activityNode.AppendChild(formNode);

                    //content = new XElement("Contents");
                    var contentsNode = xdoc.CreateElement("Contents");
                    activityNode.AppendChild(contentsNode);

                    for (int j = 0; j < teachingPlanObj.Activities.ElementAt(i).Content.Count; j++ )
                    {
                        var contentNode = xdoc.CreateElement("Content");
                        contentsNode.AppendChild(contentNode);

                        var xfrag = xdoc.CreateDocumentFragment();
                        xfrag.InnerXml = (string)teachingPlanObj.Activities.ElementAt(i).Content.ElementAt(j);
                        contentNode.AppendChild(xfrag);
                        //var childContent = new XElement("Content", "@\"" +teachingPlanObj.Activities.ElementAt(i).Content.ElementAt(j)+"\"");
                        //var childContent = new XElement("Content");
                        //childContent. 
                   
                        //content.Add(childContent);
                        //var xdoc =  new XmlDocument();
                        //xdoc.LoadXml(teachingPlanObj.Activities.ElementAt(i).Content.ElementAt(j));
                        //xdoc.Save(@"C:\test.xml");
                        //content.Add(childContent);
                    }
                    //activity.Add(content);
                    //activities.Add(activity);
                }

                //teachingplan.Add(activities);



                //var note = new XElement("ExperienceNote", teachingPlanObj.Note);
                //teachingplan.Add(note);
                //var experienceNode = xdoc.CreateElement("ExperienceNote");
                //experienceNode.InnerText = teachingPlanObj.Note;
                //teachingPlanNode.AppendChild(experienceNode);

                var experienceNode = xdoc.CreateElement("ExperienceNote");
                var xfragExperienceNode = xdoc.CreateDocumentFragment();
                xfragExperienceNode.InnerXml = (string)teachingPlanObj.Note;
                experienceNode.AppendChild(xfragExperienceNode);
                teachingPlanNode.AppendChild(experienceNode);

                xdoc.Save(filePath);

                return true;
            }
            catch (Exception ex)
            {
                Log.Debug(ex.Message + "\nLocation:[TPAccessor][Save]");
                return false;
            }
        }

        /// <summary>
        /// Open Teaching Plan Data in a TLHX file.
        /// </summary>
        /// <param name="fileName">Specify the path to open file</param>
        /// <returns>TeachingPlan</returns>
        public static TeachingPlan Load(string fileName)
        {
            TeachingPlan teachingPlan = null;
            try
            {
                //var xDoc = XMLUtilitizer.Load(fileName);
                var xDoc = XDocument.Load(fileName);
                var teachingPlanTmp =   from xElem in xDoc.Descendants("TeachingPlan")
                                        let elemComDate = xElem.Element("ComposingDate")
                                        where elemComDate != null
                                        let elemTeaDate = xElem.Element("TeachingDate")
                                        //where elemTeaDate != null
                                        let elemClass = xElem.Element("ClassLevel")
                                        //where elemClass != null
                                        let elemSubject = xElem.Element("Subject")
                                        //where elemSubject != null
                                        let elemSession = xElem.Element("Session")
                                        //where elemSession != null
                                        let elemWeek = xElem.Element("Week")
                                        //where elemWeek != null
                                        let elemTopic = xElem.Element("Topic")
                                        //where elemTopic != null
                                        let attrTopicNo = elemTopic.Attribute("Number")
                                        //where attrTopicNo != null
                                        let elemObjective = xElem.Element("Objective")
                                        //where elemObjective != null
                                        let elemPreparation = xElem.Element("Preparation")
                                        //where elemPreparation != null
                                        let elemActivities = xElem.Element("Activities")
                                        //where elemActivities != null
                                        let elemActivity = elemActivities.Elements("Activity")
                                        //where elemActivity != null
                                        let elemExperienceNote = xElem.Element("ExperienceNote")
                                        //where elemExperienceNote != null
                                        select new TeachingPlan
                                        {
                                            ComposeDate = string.IsNullOrEmpty(elemComDate.Value) ? (DateTime?)null : Convert.ToDateTime(elemComDate.Value),
                                            TeachingDate = string.IsNullOrEmpty(elemTeaDate.Value) ? (DateTime?)null : Convert.ToDateTime(elemTeaDate.Value),
                                            ClassLevel = Convert.ToInt32(elemClass.Value.Trim()),
                                            Subject = elemSubject.Value.Trim(),
                                            Session = Convert.ToInt32(elemSession.Value.Trim()),
                                            Week = Convert.ToInt32(elemWeek.Value.Trim()),
                                            Topic = elemTopic.Value.Trim(),
                                            TopicNo = string.IsNullOrEmpty(attrTopicNo.Value) ? 0 : Convert.ToInt32(attrTopicNo.Value.Trim()),
                                            Objective = elemObjective.Elements().First().ToString(),
                                            Preparation = elemPreparation.Elements().First().ToString(),
                                            Activities = ParseIEnumerable(elemActivity),
                                            //Activities = elemActivities.Value,
                                            Note = elemExperienceNote.Elements().First().ToString(),

                                        };
                return teachingPlan = new TeachingPlan
                                          {
                                              ComposeDate = teachingPlanTmp.ElementAt(0).ComposeDate,
                                              TeachingDate = teachingPlanTmp.ElementAt(0).TeachingDate,
                                              ClassLevel = teachingPlanTmp.ElementAt(0).ClassLevel,
                                              Subject = teachingPlanTmp.ElementAt(0).Subject,
                                              Session = teachingPlanTmp.ElementAt(0).Session,
                                              Week = teachingPlanTmp.ElementAt(0).Week,
                                              Topic = teachingPlanTmp.ElementAt(0).Topic,
                                              TopicNo = teachingPlanTmp.ElementAt(0).TopicNo,
                                              Objective = teachingPlanTmp.ElementAt(0).Objective,
                                              Preparation = teachingPlanTmp.ElementAt(0).Preparation,
                                              Activities = teachingPlanTmp.ElementAt(0).Activities,
                                              Note = teachingPlanTmp.ElementAt(0).Note,
                                          };

               
            }catch(Exception ex)
            {
                Log.Debug(ex.Message + "\nLocation:[TPAccessor][Load]");
            }

            return teachingPlan;
        }

        /// <summary> 
        /// help convert IEnumerable Activity to List of Activity
        /// </summary>
        public static List<Activity> ParseIEnumerable(IEnumerable<XElement> iEnumerable)
        {
            var lstActivity = new List<Activity>();
            try
            {
                //var arrSubject = iEnumerable.ToArray();

                foreach (var element in iEnumerable)
                {
                    //var strActivity = element.ToString().Replace("<Activity>", "").Replace(
                    //    "</Activity>", "").Trim();
                    //var activity = new Activity
                    //{
                    //    Name = GetSubString(strActivity, "<Name>", "</Name>"),
                    //    Purpose = GetSubString(strActivity, "<Purpose>", "</Purpose>"),
                    //    Form = GetSubString(strActivity, "<Form>", "</Form>"),
                    //    Content = ParseActivityContent(GetSubString(strActivity, "<Contents>", "</Contents>").Trim()),
                    //    //TeamSize = GetSubString(strActivity, "<Teamsize>", "</Teamsize>"),
                    //    //Content = GetSubString(strActivity, "<Content>", "</Content>"),
                    //};
                    var temp = element.Elements();
               
                    var activity = new Activity
                    {
                        Name = temp.ElementAt(0).Value,
                        Purpose = temp.ElementAt(1).Value,
                        Form = temp.ElementAt(2).Value,
                        Content = ParseActivityContent(temp.ElementAt(3).Elements()),
                        //TeamSize = GetSubString(strActivity, "<Teamsize>", "</Teamsize>"),
                        //Content = GetSubString(strActivity, "<Content>", "</Content>"),
                    };
                    lstActivity.Add(activity);
                }

            }
            catch (Exception ex)
            {
                Log.Debug(ex.Message + "\nLocation:[TPAccessor][ParseIEnumerable]");
            }
            return lstActivity;
        }

        public static List<object> ParseActivityContent(IEnumerable<XElement> iEnumerable)
        {
            var lstContent = new List<object>();
            try
            {
                //string[] arrContent = Regex.Split(strContent.Trim(), "</Content>");

                //for (int i = 0; i < arrContent.Count() - 1; i++)
                //{
                //    arrContent[i] = arrContent[i].Replace("<Content>", "").Trim();
                //    if(!string.IsNullOrEmpty(arrContent[i]))
                //    {
                //        lstContent.Add(arrContent[i]);
                //    }
                //}
                foreach (var element in iEnumerable)
                {
                    lstContent.Add(element.Elements().First().ToString());
                }
            }
            catch (Exception ex)
            {
                Log.Debug(ex.Message + "\nLocation:[TPAccessor][ParseActivityContent]");
            }
            return new List<object>(lstContent);
        }
      
        /// <summary>
        /// Export to chosen file type
        /// </summary>
        /// <param name="teachingPlan">Specify the Teaching Plan Object</param>
        /// <param name="fileType">Specify the file type</param>
        /// <param name="fileName">Specify the file name</param>
        /// <param name="lang">Specify the language</param>
        /// <returns>Boolean</returns>
        public static bool ExportTeachingPlan(TeachingPlan teachingPlan, string fileType, string fileName, string lang)
        {
            try
            {
                var rtbExport = new RichTextBox();
                //var rtbHeader = new RichTextBox();
                //var rtbObjectiveLabel = new RichTextBox();
                //var rtbPreparationLabel = new RichTextBox();
                //var rtbActivityLabel = new RichTextBox();
                //var rtbActivityPurpose = new RichTextBox();
                //var rtbActivityForm = new RichTextBox();
                //var rtbActivityContent = new RichTextBox();
                //var rtbExperienceLabel = new RichTextBox();
                //fileName = fileName.Substring(0, fileName.Length - 5);
                fileName = fileName.Replace(".docx","").Replace(".doc","").Replace(".rtf","").Replace(".xps","").Replace(".pdf","");
                

                // Compose Date
                string comDate = null;
                if (teachingPlan.ComposeDate == null)
                {
                    comDate = "";
                }
                else if (string.IsNullOrEmpty(teachingPlan.ComposeDate.Value.ToString(CultureInfo.InvariantCulture)))
                {
                    comDate = "";
                }
                else
                {
                    comDate = teachingPlan.ComposeDate.Value.ToString(lang.Trim().Equals("en-US") ? "MM/dd/yyyy" : "dd/MM/yyyy");
                }

                // Teaching Date
                string teaDate = null;
                if (teachingPlan.TeachingDate == null)
                {
                    teaDate = "";
                }
                else if (string.IsNullOrEmpty(teachingPlan.TeachingDate.Value.ToString(CultureInfo.InvariantCulture)))
                {
                    teaDate = "";
                }
                else
                {
                    teaDate = teachingPlan.TeachingDate.Value.ToString(lang.Trim().Equals("en-US") ? "MM/dd/yyyy" : "dd/MM/yyyy");
                }

                //English Template
                //if (lang.Trim().Equals("en-EN"))
                //{
                #region Template

                #region Header


                //var rtbHeader = new RichTextBox();
                var paraComposingDate = new Paragraph
                {
                    TextAlignment = TextAlignment.Center,
                    FontFamily = new FontFamily("Times New Roman")
                };

                //Compose Date
                var runComposeDate = new Run
                {
                    Text = lang.Trim().Equals("en-US") ? "Composing Date: " + comDate : "Ngày Soạn: " + comDate,
                    BaselineAlignment = BaselineAlignment.Center,
                    FontSize = 16,
                    FontWeight = FontWeights.Normal,
                    FontStyle = FontStyles.Italic,
                    Foreground = new SolidColorBrush(Colors.Black)
                };
                paraComposingDate.Inlines.Add(runComposeDate);
                rtbExport.Document.Blocks.Add(paraComposingDate);
                //paraHeader.Inlines.Add(new LineBreak());
                var paraTeachingDate = new Paragraph
                {
                    TextAlignment = TextAlignment.Center,
                    FontFamily = new FontFamily("Times New Roman")
                };

                //Teaching Date
                var runTeaDate = new Run
                {
                    Text = lang.Trim().Equals("en-US") ? "Teaching Date: " + teaDate : "Ngày Dạy: " + teaDate,
                    BaselineAlignment = BaselineAlignment.Center,
                    FontSize = 16,
                    FontWeight = FontWeights.Normal,
                    FontStyle = FontStyles.Italic,
                    Foreground = new SolidColorBrush(Colors.Black)
                };
                paraTeachingDate.Inlines.Add(runTeaDate);
                //paraHeader.Inlines.Add(new LineBreak());
                rtbExport.Document.Blocks.Add(paraTeachingDate);
                var paraSubject = new Paragraph
                {
                    TextAlignment = TextAlignment.Center,
                    FontFamily = new FontFamily("Times New Roman")
                };

                var runSubject = new Run
                {
                    Text = teachingPlan.Subject,
                    BaselineAlignment = BaselineAlignment.Center,
                    FontSize = 17.5,
                    FontWeight = FontWeights.Bold,
                    Foreground = new SolidColorBrush(Colors.Black)
                };
                //runSubject.FontStyle = FontStyles.Normal;
                paraSubject.Inlines.Add(runSubject);
                //paraSubject.Inlines.Add(new LineBreak());
                rtbExport.Document.Blocks.Add(paraSubject);
                var paraSession = new Paragraph
                {
                    TextAlignment = TextAlignment.Center,
                    FontFamily = new FontFamily("Times New Roman")
                };
                //Session
                var runSession = new Run
                {
                    Text = lang.Trim().Equals("en-US") ? "Session " + teachingPlan.Session + ":" : "Tiết " + teachingPlan.Session + ":",
                    BaselineAlignment = BaselineAlignment.Center,
                    FontSize = 17.5,
                    FontWeight = FontWeights.Bold,
                    Foreground = new SolidColorBrush(Colors.Black)
                };
                //runSession.FontStyle = FontStyles.Normal;
                paraSession.Inlines.Add(runSession);

                //Topic
                var runTopic = new Run
                {
                    Text = teachingPlan.Topic,
                    BaselineAlignment = BaselineAlignment.Center,
                    FontSize = 17.5,
                    FontWeight = FontWeights.Bold,
                    Foreground = new SolidColorBrush(Colors.Black),
                };
                runTopic.Typography.Capitals = FontCapitals.SmallCaps;

                //runTopic.FontStyle = FontStyles.Normal;
                paraSession.Inlines.Add(runTopic);
                //rtbHeader.Document.Blocks.Add(paraHeader);
                rtbExport.Document.Blocks.Add(paraSession);

                #endregion

                #region Objective

                //Objective label
                //var rtbObjectiveLabel = new RichTextBox();
                var paraObjectiveLabel = new Paragraph
                {
                    TextAlignment = TextAlignment.Left,
                    FontFamily = new FontFamily("Times New Roman")
                };
                var runObjectiveLabel = new Run
                {
                    Text = lang.Trim().Equals("en-US") ? "I/ Objective:" : "I/ Mục Tiêu:",
                    BaselineAlignment = BaselineAlignment.Center,
                    FontSize = 17.5,
                    FontWeight = FontWeights.Bold,
                    Foreground = new SolidColorBrush(Colors.Black)
                };
                //runTopic.FontStyle = FontStyles.Normal;
                paraObjectiveLabel.Inlines.Add(runObjectiveLabel);
                //rtbObjectiveLabel.Document.Blocks.Add(paraObjectiveLabel);
                rtbExport.Document.Blocks.Add(paraObjectiveLabel);
                //var rtbObjective = new RichTextBox { Document = RichTextBoxProcessor.ConvertToFlowDocument(teachingPlan.Objective) };
                var document = ((FlowDocument)teachingPlan.Objective);
                //if (document.Blocks.Count < 1)
                //{
                //    var runObj = new Run { Text = " " };
                //    var paraObj = new Paragraph();
                //    paraObj.Inlines.Add(runObj);
                //    document.Blocks.Add(paraObj);
                //}
                //foreach (var block in rtbObjective.Document.Blocks)
                //{
                rtbExport.Document.Blocks.AddRange(document.Blocks.ToArray());
                //}
                //rtbObjective.SelectAll();
                //rtbObjective.Copy();
                //rtbExport.Paste();
                #endregion

                #region Preparation

                //Preparation label
                //var rtbPreparationLabel = new RichTextBox();
                var paraPreparationLabel = new Paragraph
                {
                    TextAlignment = TextAlignment.Left,
                    FontFamily = new FontFamily("Times New Roman")
                };
                var runPreparationLabel = new Run
                {
                    Text = lang.Trim().Equals("en-US") ? "II/ Preparation:" : "II/ Chuẩn Bị:",
                    BaselineAlignment = BaselineAlignment.Center,
                    FontSize = 17.5,
                    FontWeight = FontWeights.Bold,
                    Foreground = new SolidColorBrush(Colors.Black)
                };
                //runTopic.FontStyle = FontStyles.Normal;
                paraPreparationLabel.Inlines.Add(runPreparationLabel);
                //rtbPreparationLabel.Document.Blocks.Add(paraPreparationLabel);
                rtbExport.Document.Blocks.Add(paraPreparationLabel);
                //var rtbPreparation = new RichTextBox
                //{
                document = ((FlowDocument)teachingPlan.Preparation);
                //if (document.Blocks.Count < 1)
                //{
                //    var runPre = new Run { Text = " " };
                //    var paraPre = new Paragraph();
                //    paraPre.Inlines.Add(runPre);
                //    document.Blocks.Add(paraPre);
                //}
                //};
                rtbExport.Document.Blocks.AddRange(document.Blocks.ToArray());
                #endregion

                #region Activities

                //Activities label
                //var rtbActivityLabel = new RichTextBox();
                var paraActivityLabel = new Paragraph
                                            {
                                                TextAlignment = TextAlignment.Left,
                                                FontFamily = new FontFamily("Times New Roman")
                                            };
                var runActivityLabel = new Run
                                           {
                                               Text =
                                                   lang.Trim().Equals("en-US")
                                                       ? "III/ Activities:"
                                                       : "III/ Hoạt Động Dạy Học:",
                                               BaselineAlignment = BaselineAlignment.Center,
                                               FontSize = 17.5,
                                               FontWeight = FontWeights.Bold,
                                               Foreground = new SolidColorBrush(Colors.Black)
                                           };
                //runTopic.FontStyle = FontStyles.Normal;
                paraActivityLabel.Inlines.Add(runActivityLabel);
                //rtbActivityLabel.Document.Blocks.Add(paraActivityLabel);
                rtbExport.Document.Blocks.Add(paraActivityLabel);

                int count = 0;
                foreach (var activity in teachingPlan.Activities)
                {
                    count++;
                    //var rtbActivityHeader = new FlowDocument();
                    var paraActivityName = new Paragraph
                    {
                        TextAlignment = TextAlignment.Left,
                        FontFamily = new FontFamily("Times New Roman")
                    };
                    var runActivityName = new Run
                    {
                        Text = lang.Trim().Equals("en-US") ? count + ") " + "Activity " + count + ": " + activity.Name : "Hoạt động " + count + ": " + activity.Name,
                        BaselineAlignment = BaselineAlignment.Center,
                        FontSize = 17.5,
                        FontWeight = FontWeights.Bold,
                        Foreground = new SolidColorBrush(Colors.Black)
                    };
                    paraActivityName.Inlines.Add(runActivityName);
                    //paraActivityName.Inlines.Add(new LineBreak());
                    rtbExport.Document.Blocks.Add(paraActivityName);
                    var paraActivityPurpose = new Paragraph
                    {
                        TextAlignment = TextAlignment.Left,
                        FontFamily = new FontFamily("Times New Roman")
                    };
                    var runActivityPurpose = new Run
                    {
                        Text = lang.Trim().Equals("en-US") ? "  + Purpose: " + activity.Purpose : "  + Mục đích: " + activity.Purpose,
                        BaselineAlignment = BaselineAlignment.Center,
                        FontSize = 17.5,
                        FontWeight = FontWeights.Normal,
                        Foreground = new SolidColorBrush(Colors.Black)
                    };
                    paraActivityPurpose.Inlines.Add(runActivityPurpose);
                    //paraActivityHeader.Inlines.Add(new LineBreak());
                    rtbExport.Document.Blocks.Add(paraActivityPurpose);
                    var paraActivityForm = new Paragraph
                    {
                        TextAlignment = TextAlignment.Left,
                        FontFamily = new FontFamily("Times New Roman")
                    };
                    var runActivityForm = new Run
                    {
                        Text = lang.Trim().Equals("en-US") ? "  + Team Size: " + activity.Form : "  + Hình thức: " + activity.Form,
                        BaselineAlignment = BaselineAlignment.Center,
                        FontSize = 17.5,
                        FontWeight = FontWeights.Normal,
                        Foreground = new SolidColorBrush(Colors.Black)
                    };
                    paraActivityForm.Inlines.Add(runActivityForm);
                    //paraActivityForm.Inlines.Add(new LineBreak());
                    rtbExport.Document.Blocks.Add(paraActivityForm);
                    var paraActivityContent = new Paragraph
                    {
                        TextAlignment = TextAlignment.Left,
                        FontFamily = new FontFamily("Times New Roman")
                    };
                    var runActivityContent = new Run
                    {
                        Text = lang.Trim().Equals("en-US") ? "  + Content: " : "  + Nội dung: ",
                        BaselineAlignment = BaselineAlignment.Center,
                        FontSize = 17.5,
                        FontWeight = FontWeights.Normal,
                        Foreground = new SolidColorBrush(Colors.Black)
                    };
                    paraActivityContent.Inlines.Add(runActivityContent);
                    //rtbActivityHeader.Blocks.Add(paraActivityHeader);
                    rtbExport.Document.Blocks.Add(paraActivityContent);
                    //rtbExport.Document.Blocks.AddRange(rtbActivityHeader.Blocks);
                    //rtbActivityHeader.SelectAll();
                    //rtbActivityHeader.Copy();
                    //rtbExport.Paste();
                    //Clipboard.Clear();
                    //Clipboard.SetText(""););

                    for (int i = 0; i < activity.Content.Count; i++)
                    {
                        //var rtbActivityContent = new RichTextBox
                        //{
                        //    Document =
                        //        RichTextBoxProcessor.ConvertToFlowDocument(
                        //            activity.Content.ElementAt(i))
                        //};

                        //rtbActivityContent.SelectAll();
                        //rtbActivityContent.Copy();
                        //rtbExport.Paste();
                        //Clipboard.Clear();
                        //Clipboard.SetText("");
                       // for (int j = 0; j < ((List<RichTextBox>)activity.Content.ElementAt(i)).Count; j++)
                        //{
                            //((List<RichTextBox>)activity.Content.ElementAt(i)).ElementAt(j).SelectAll();
                            //((List<RichTextBox>)activity.Content.ElementAt(i)).ElementAt(j).Copy();
                            //rtbExport.Paste();
                            rtbExport.Document.Blocks.AddRange(((FlowDocument)activity.Content.ElementAt(i)).Blocks.ToArray());
                        //}


                    }


                }
                #endregion

                #region Experience

                //Experience
                //var rtbExperienceLabel = new RichTextBox();
                var paraExperienceLabel = new Paragraph
                                              {
                                                  TextAlignment = TextAlignment.Left,
                                                  FontFamily = new FontFamily("Times New Roman")
                                              };
                var runExperienceLabel = new Run
                                             {
                                                 Text =
                                                     lang.Trim().Equals("en-US")
                                                         ? "IV/ Experience Note:"
                                                         : "IV/ Rút Kinh Nghiệm:",
                                                 BaselineAlignment = BaselineAlignment.Center,
                                                 FontSize = 17.5,
                                                 FontWeight = FontWeights.Bold,
                                                 Foreground = new SolidColorBrush(Colors.Black)
                                             };
                //runTopic.FontStyle = FontStyles.Normal;
                paraExperienceLabel.Inlines.Add(runExperienceLabel);
                rtbExport.Document.Blocks.Add(paraExperienceLabel);
                //rtbExport.Document.Blocks.Add(paraExperienceLabel);
                #endregion

                #endregion
                //}
                ////Vietnamese Template 
                //else if (lang.Trim().Equals("vi-VN"))
                //{

                //}
                //var rtbObjective = new RichTextBox
                //                       {Document = RichTextBoxProcessor.ConvertToFlowDocument(teachingPlan.Objective)};
                //var rtbPreparation = new RichTextBox
                //                         {
                //                             Document =
                //                                 RichTextBoxProcessor.ConvertToFlowDocument(teachingPlan.Preparation)
                //                         };
                //var rtbActivities = new RichTextBox
                //                        {Document = RichTextBoxProcessor.ConvertToFlowDocument(teachingPlan.Activities)};


                document = ((FlowDocument)teachingPlan.Note);
                //if (document.Blocks.Count < 1)
                //{
                //    var runNote = new Run { Text = " " };
                //    var paraNote = new Paragraph();
                //    paraNote.Inlines.Add(runNote);
                //    document.Blocks.Add(paraNote);
                //}
                rtbExport.Document.Blocks.AddRange(document.Blocks.ToArray());
                //rtbHeader.SelectAll();
                //rtbHeader.Copy();
                //rtbExport.Paste();
                //Clipboard.Clear();
                //Clipboard.SetText("");


                //rtbObjectiveLabel.SelectAll();
                //rtbObjectiveLabel.Copy();
                //rtbExport.Paste();
                //Clipboard.Clear();
                //Clipboard.SetText("");

                //if(rtbObjective.Document.Blocks.Count < 1)
                //{
                //    var runObj = new Run {Text = " "};
                //    var paraObj = new Paragraph();
                //    paraObj.Inlines.Add(runObj);
                //    rtbObjective.Document.Blocks.Add(paraObj);
                //}
                //rtbObjective.SelectAll();
                //rtbObjective.Copy();
                //rtbExport.Paste();
                //Clipboard.Clear();
                //Clipboard.SetText("");

                
                //rtbPreparationLabel.SelectAll();
                //rtbPreparationLabel.Copy();
                //rtbExport.Paste();
                //Clipboard.Clear();
                //Clipboard.SetText("");

                //if (rtbPreparation.Document.Blocks.Count < 1)
                //{
                //    var runPre = new Run { Text = " " };
                //    var paraPre = new Paragraph();
                //    paraPre.Inlines.Add(runPre);
                //    rtbPreparation.Document.Blocks.Add(paraPre);
                //}
                //rtbPreparation.SelectAll();
                //rtbPreparation.Copy();
                //rtbExport.Paste();
                //Clipboard.Clear();
                //Clipboard.SetText("");

                //rtbActivityLabel.SelectAll();
                //rtbActivityLabel.Copy();
                //rtbExport.Paste();
                //Clipboard.Clear();
                //Clipboard.SetText("");

                //rtbActivities.SelectAll();
                //rtbActivities.Copy();
                //rtbExport.Paste();
                //Clipboard.Clear();
                //int count = 0;
                //foreach (var activity in teachingPlan.Activities)
                //{
                //    count++;
                //    var rtbActivityHeader = new RichTextBox();
                //    var paraActivityHeader = new Paragraph
                //    {
                //        TextAlignment = TextAlignment.Left,
                //        FontFamily = new FontFamily("Times New Roman")
                //    };
                //    var runActivityName = new Run
                //    {
                //        Text = lang.Trim().Equals("en-US") ? count + ") " + "Activity " + count + ": " + activity.Name : "Hoạt động " + count + ": " + activity.Name,
                //        BaselineAlignment = BaselineAlignment.Center,
                //        FontSize = 14,
                //        FontWeight = FontWeights.Bold,
                //        Foreground = new SolidColorBrush(Colors.Black)
                //    };
                //    paraActivityHeader.Inlines.Add(runActivityName);
                //    paraActivityHeader.Inlines.Add(new LineBreak());


                //    var runActivityPurpose = new Run
                //    {
                //        Text = lang.Trim().Equals("en-US") ? "  +Purpose: " + activity.Purpose : "  +Mục đích: " + activity.Purpose,
                //        BaselineAlignment = BaselineAlignment.Center,
                //        FontSize = 14,
                //        FontWeight = FontWeights.Normal,
                //        Foreground = new SolidColorBrush(Colors.Black)
                //    };
                //    paraActivityHeader.Inlines.Add(runActivityPurpose);
                //    paraActivityHeader.Inlines.Add(new LineBreak());

                //    var runActivityForm = new Run
                //    {
                //        Text = lang.Trim().Equals("en-US") ? "  +Form: " + activity.Form : "  +Hình thức: " + activity.Form,
                //        BaselineAlignment = BaselineAlignment.Center,
                //        FontSize = 14,
                //        FontWeight = FontWeights.Normal,
                //        Foreground = new SolidColorBrush(Colors.Black)
                //    };
                //    paraActivityHeader.Inlines.Add(runActivityForm);
                //    paraActivityHeader.Inlines.Add(new LineBreak());

                //    var runActivityContent = new Run
                //    {
                //        Text = lang.Trim().Equals("en-US") ? "  +Content: " : "  +Nội dung: ",
                //        BaselineAlignment = BaselineAlignment.Center,
                //        FontSize = 14,
                //        FontWeight = FontWeights.Normal,
                //        Foreground = new SolidColorBrush(Colors.Black)
                //    };
                //    paraActivityHeader.Inlines.Add(runActivityContent);
                //    rtbActivityHeader.Document.Blocks.Add(paraActivityHeader);

                //    rtbActivityHeader.SelectAll();
                //    rtbActivityHeader.Copy();
                //    rtbExport.Paste();
                //    //Clipboard.Clear();
                //    //Clipboard.SetText("");

                //    for (int i = 0; i < activity.Content.Count; i++)
                //    {
                //        //var rtbActivityContent = new RichTextBox
                //        //{
                //        //    Document =
                //        //        RichTextBoxProcessor.ConvertToFlowDocument(
                //        //            activity.Content.ElementAt(i))
                //        //};

                //        //rtbActivityContent.SelectAll();
                //        //rtbActivityContent.Copy();
                //        //rtbExport.Paste();
                //        //Clipboard.Clear();
                //        //Clipboard.SetText("");
                //        for (int j = 0; j < ((List<RichTextBox>)activity.Content.ElementAt(i)).Count; j++ )
                //        {
                //            ((List<RichTextBox>)activity.Content.ElementAt(i)).ElementAt(j).SelectAll();
                //            ((List<RichTextBox>)activity.Content.ElementAt(i)).ElementAt(j).Copy();
                //            rtbExport.Paste();
                //        }
                        

                //    }


                //}



                //rtbExperienceLabel.SelectAll();
                //rtbExperienceLabel.Copy();
                //rtbExport.Paste();
                //Clipboard.Clear();
                //Clipboard.SetText("");

                //if (rtbNote.Document.Blocks.Count < 1)
                //{
                //    var runNote = new Run { Text = " " };
                //    var paraNote = new Paragraph();
                //    paraNote.Inlines.Add(runNote);
                //    rtbNote.Document.Blocks.Add(paraNote);
                //}
                //rtbNote.SelectAll();
                //rtbNote.Copy();
                //rtbExport.Paste();
                //Clipboard.Clear();
                //Clipboard.SetText("");





                //string strHeader = RichTextBoxProcessor.ConvertToString(rtbHeader);
                //string strObjective = RichTextBoxProcessor.ConvertToString(rtbObjectiveLabel) + teachingPlan.Objective;
                //string strPreparation = RichTextBoxProcessor.ConvertToString(rtbPreparationLabel) + teachingPlan.Preparation;
                ////string strActivity = RichTextBoxProcessor.ConvertToString(rtbActivityLabel) + teachingPlan.Activities;
                //string strExperience = RichTextBoxProcessor.ConvertToString(rtbExperienceLabel) + teachingPlan.Note;
                //string strExport = strHeader + strObjective + strPreparation  + strExperience;
                //rtbExport.Document = RichTextBoxProcessor.ConvertToFlowDocument(strExport);


                if (FileExporter.Export(rtbExport, fileType, fileName))
                {
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                Log.Debug(ex.Message + "\nLocation:[TPAccessor][ExportTeachingPlan]");
            }

            return false;

        }

        #endregion

        
    }
}
